package net.zjitc.dao.impl;

import net.zjitc.dao.UserDao;
import net.zjitc.domain.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/2/27.
 */
@Repository
public class UserDaoImpl implements UserDao {
    @Resource
    private SessionFactory sessionFactory;
    protected  Session getSession(){
        Session session = sessionFactory.getCurrentSession();
        return session;
    }
    @Override
    public User findByNamePassword(String username, String password) {
       /* Session session = sessionFactory.getCurrentSession();*//*openSession()*/
        /*Transaction transaction = session.beginTransaction();*/
        User user = null;
        user = (User) getSession().createQuery("from User u where u.username=? and u.password=?")
                .setParameter(0,username).setParameter(1,password).uniqueResult();

        return user;
    }

    @Override
    public List<User> findAll() {
        return getSession().createQuery("from User ").list();
    }
}
